/*$(document).ready(function () {
   */

$(document).ready(function () {
    var query = {
    "contentSourceType" : $('#contentSourceType option:selected').val(),
    "sender" : $.trim($('#sender').val()),
    "sendStartTime" : $('#start').val(),
    "sendEndTime": $('#end').val(),
    "contentType": contentType,
    "censorStatus":statusGlobal,
    "censorLevel":levelGlobal,
    "page": 1,
    "limit": PageSize};
    pictureSearch(query);

    function appentStr(result){
        var text = ""
        var currentCensorLevel = '未定级';
        var currentPhone = '暂无';
        for (i = 0; i < result.rows.length; i++){
            if(result.rows[i].censorLevel != 0){
                currentCensorLevel = result.rows[i].censorLevel;
            }
            if(result.rows[i].senderPhoneEcrypted !=''){
                currentPhone = result.rows[i].senderPhoneEcrypted;
            }

            var str1 =
                "<div class='file-box'>\n" +
                "                    <div class='file'>\n" +
                "<div class='glyphicon glyphicon-ok-sign yes' id ='check' value='"+result.rows[i].id+"'></div>\n"+
                "                            <div class='fancybox' href='"+result.rows[i].contentUrl+"'>\n" +
                "                                <img alt='image' class='myImage' src='"+result.rows[i].contentUrl+"'>\n" +
                "                            </div>\n<div>\n" +
                "                                    <div class='time col-sm-12'><font style='color:white'>"+CommonUtils.formateDate(result.rows[i].sendTime)+"</font></div>"+
                "                                    <div><button class='btn col-sm-3 censorLevel'> <small>\n"+currentCensorLevel+"级</small></button>\n" +
                "                                     <button class='btn col-sm-3'><small>\n"+result.rows[i].sender+"</small></button>\n" +
                "                                     <button class='btn col-sm-6'><small>\n"+currentPhone+"</small></button></div><div >\n" +
                "                                     <button id='pictureLevel' class='btn col-sm-3' value='1'><small>1级</small></button>\n" +
                "                                     <button id='pictureLevel' class='btn col-sm-3' value='2'><small>2级</small></button>\n" +
                "                                     <button id='pictureLevel' class='btn col-sm-3' value='3'><small>3级</small></button>\n" +
                "                                     <button id='pictureLevel' class='btn col-sm-3' value='4'><small>4级</small></button>\n" +
                "                            </div>\n" +
                "                    </div></div>\n" +
                "                </div>";
            text = text + str1;
        }
        return text;
    };

    function pictureSearch(query) {
        $.ajax({
            type: "post",
            url: "/censor/getAllCensorContent",
            dataType: "json",
            //contentType : "application/json",
            contentType : "application/x-www-form-urlencoded",
            data: query,
            success:function (result) {

                $("#pic").html(appentStr(result));
                $("#checkAll").text("全选");
                var currentPage = 1; //当前页数
                var pageCount = Math.ceil(result.total/query.limit); //总页数
                var options = {
                    currentPage: currentPage, //当前页
                    totalPages: pageCount, //总页数
                    numberOfPages: 5, //设置控件显示的页码数
                    bootstrapMajorVersion: 3,
                    useBootstrapTooltip: false,
                    itemTexts:function(type,page, current){//文字翻译
                        switch (type) {
                            case "first":
                                return "首页";
                            case "prev":
                                return "上一页";
                            case "next":
                                return "下一页";
                            case "last":
                                return "尾页";
                            case "page":
                                return page;
                        }
                    },//点击事件。ajax 刷新整个列表
                    onPageClicked: function (event, originalEvent, type, page) {
                        //给每个页眉绑定一个事件，其实就是ajax请求
                        var query1 = {
                            "contentSourceType" : $('#contentSourceType option:selected').val(),
                            "sender" : $.trim($('#sender').val()),
                            "sendStartTime" : $('#start').val(),
                            "sendEndTime": $('#end').val(),
                            "contentType": contentType,
                            "page": page,
                            "limit": PageSize};
                        $.ajax({
                            type: "post",
                            url: "/censor/getAllCensorContent",
                            dataType: "json",
                            //contentType : "application/json",
                            contentType : "application/x-www-form-urlencoded",
                            data: query1,
                            success:function (data1) {
                                if(data1 != null){
                                    $("#pic").html(appentStr(data1));
                                    $("#checkAll").text("全选");
                                }
                            }
                        });
                    }
                };
                $('#pageLimit').bootstrapPaginator(options);
            }
        });
    }
    //单击查询事件
    $("#pictureSearch").click(function () {
        var query2 = {
            "contentSourceType" : $('#contentSourceType option:selected').val(),
            "sender" : $.trim($('#sender').val()),
            "sendStartTime" : $('#start').val(),
            "sendEndTime": $('#end').val(),
            "contentType": contentType,
            "censorStatus":statusGlobal,
            "censorLevel":levelGlobal,
            "page": 1,
            "limit": PageSize};
        alert("单击查询事件");
        pictureSearch(query2);
    });
    //单击状态事件
    $(".status").click(function () {
        $(this).addClass('btn-primary').siblings().removeClass('btn-primary');
        statusGlobal = $(this).val();
        var queryStatus = {
            "contentSourceType" : $('#contentSourceType option:selected').val(),
            "sender" : $.trim($('#sender').val()),
            "sendStartTime" : $('#start').val(),
            "sendEndTime": $('#end').val(),
            "contentType": contentType,
            "censorStatus":statusGlobal,
            "censorLevel":levelGlobal,
            "page": 1,
            "limit": PageSize};
        pictureSearch(queryStatus);

    })
    //单击等级事件
    $(".level").click(function () {
        alert("111")
        $(this).addClass('btn-primary').siblings().removeClass('btn-primary');
        levelGlobal = $(this).val();
        var queryLevel = {
            "contentSourceType" : $('#contentSourceType option:selected').val(),
            "sender" : $.trim($('#sender').val()),
            "sendStartTime" : $('#start').val(),
            "sendEndTime": $('#end').val(),
            "contentType": contentType,
            "censorStatus":statusGlobal,
            "censorLevel":levelGlobal,
            "page": 1,
            "limit": PageSize};
        pictureSearch(queryLevel);
    });
    //缩放事件
    $('.fancybox').fancybox({
        openEffect: 'none',
        closeEffect: 'none'
    });
    //单击选中图片
    $("body").on("click", ".glyphicon-ok-sign", function (){
        var checkedStatus = $(this).attr("checked");
        if( $(this).attr("checked") == 'checked'){
            $(this).css("color","");
            $(this).attr("checked",false)
        }else{
            $(this).css("color","#1d9d74");
            $(this).attr("checked",true);
        }
        //var checkedId = $(this).attr("value");

    });
    //单击选中等级事件
    $("body").on("click", "#pictureLevel", function (){
        var _this = $(this); //ajax 中使用this需外部先定义
        var singleCheckedId = $(this).parent().parent().prevAll(".yes").attr("value");
        var singleCheckedStatus = $(this).parent().parent().prevAll(".yes").attr("checked");
        var singleCheckedLevel = $(this).attr("value");
       /* var data_list = new Array();
        data_list.push(singleCheckedId)*/
        if(singleCheckedStatus != 'checked'){
            layer.msg('未选中图片，请重新选中');
        }else{
            layer.confirm( "确认修改等级为" + singleCheckedLevel + "级吗?",
                {icon: 3, title: '提示'},
                function () {
                    var data_list = new Array();
                    data_list.push(singleCheckedId);
                    var data = {
                        /*"id": singleCheckedId,*/
                        "censorLevel": singleCheckedLevel,
                        "list":data_list
                    };
                    $.ajax({
                        type: "post",
                        url: "/censor/updateBatch",//updateBatch
                        dataType: "json",
                        contentType : 'application/json',
                        data: JSON.stringify(data),
                        success: function (result){
                            if (result.code === '0') {
                                layer.msg('审核成功');
                               //TODO 写入修改后数据
                                //1、直接修改页面数据
                                _this.parent().prev("div").find(".censorLevel").find("small").html(singleCheckedLevel);
                                //2、重新执行查询方法。

                            } else {
                                layer.alert("操作失败", {icon: 5, time: 2000, title: '提示'});
                            }
                        },
                        error: function (result) {
                            layer.alert("未知错误,请刷新数据查看", {icon: 0, time: 1000, title: '提示'});
                        }
                    });

                },
                function () {
                    layer.close();
                }
                );
        }

    });
    $("#checkAll").click(function () {
        var text = $("#checkAll").text();
        if(text=="全选"){
            $("div .glyphicon").each(function () {
                $(this).css("color","#1d9d74");
                $(this).attr("checked",true);
                console.info($(this).attr("value"));
            })
           //选中所有图片
            $("#checkAll").text("取消全选");}
        if(text=="取消全选") {
            $("div .glyphicon").each(function () {
                $(this).css("color", "");
                $(this).attr("checked", false)
            })
            $("#checkAll").text("全选");
        }
    });
    //批处理事件
    $(".patch").click(function () {
        //获取所有已选择的列
        var _this = $(this);
        var checkedLevel = $(this).val();
        console.info("checkedLevel"+checkedLevel);
        var data_list = new Array();
        $("div .glyphicon").each(function () {
            if($(this).attr("checked") == 'checked'){
                //将选中的数据添加到list中
                data_list.push($(this).attr("value"));
            }
        })
        if(data_list.length == 0){
            layer.msg('未选择数据');
        }else{
            //进行批处理操作
            console.info("进行批处理操作"+checkedLevel);

            var data ={
                "censorLevel":checkedLevel,
                "list":data_list
            };
            if(checkedLevel === "0"){
                var message ="是否确认选中部分全部修保持现有等级级?"
            }else{
                message ="是否确认选中部分全部修改为" + checkedLevel + "级?"
            }
            layer.confirm(
                message,
                {icon: 3, title: '提示'},
                function(index){
                    $.ajax({
                        type: "post",
                        url: "/censor/updateBatch",
                        dataType: "json",
                        contentType : 'application/json',
                        data: JSON.stringify(data),
                        success:function(result){
                            if (result.code === '0') {
                                layer.msg('审核成功');

                                //1、将页面数据修改为选中的审核等级。
                                    //遍历选中的图片
                                if(checkedLevel ==0){
                                    layer.msg('审核成功,保持现有等级');
                                    return false;
                                }else{
                                    $("div .glyphicon").each(function () {
                                        if($(this).attr("checked") == 'checked'){
                                            //将原来的等级修改为选中的审核等级
                                            $(this).nextAll().find("div").find(".censorLevel").find("small").html(checkedLevel);
                                        }
                                    })
                                }

                                //2、重新执行查询语句。
                            } else {
                                layer.alert("操作失败", {icon: 5, time: 2000, title: '提示'});
                            }
                            $("#checkAll").text("全选");
                        }
                    });
                    layer.close(index);
                    //取消选择数据
                },
                function (index) {
                    layer.close(index);
                }
            );

        }


    })




});